
#ifndef _DO_EVENT_H
#define _DO_EVENT_H
#include "draw.h"
#include <unistd.h>
#include "page.h"
#include "music.h"

#include <pthread.h>
#include "movie.h"
#include "game.h"

void do_mouse_event(int, int, int *, int *);
void home_event(int, int, int *);
void music_event(int, int, int *, int *);
void movie_event(int, int, int *);
void game_event(int, int, int *);
void cleardate();

// eventflag
// 100 - 200  music    start 101     pause 151

// 200 - 300 movie       no use
// 300 - 400 game        no use

void do_mouse_event(int x, int y, int *flag, int *eventflag)
{

  switch (*flag)
  {
  case 0:
    home_event(x, y, flag);
    break;
  case 1:
    music_event(x, y, flag, eventflag);
    break;
  case 2:
    movie_event(x, y, flag);
    break;
  case 3:
    game_event(x, y, flag);
    break;
  default:
    break;
  }
}


//turn who  white or black to play
static int who;

//the chessboard  default 0  if black  play chess then the point at this array will be 1 , if white then 2;
// according to this array to judge who win
static int qp[14][14];

//means this turn of game ending or not, or click back
static int over;

void cleardate()
{
  who = 0;
  over = 0;
  for (int i = 0; i < 14; i++)
  {
    for (int j = 0; j < 14; j++)
    {
      qp[j][i] = 0;
    }
  }
}

void game_event(int x, int y, int *flag)
{

  if (x >= 0 && x <= 50 && y >= 0 && y <= 50)
  {
    // back event
    display_bmp("./bmp/game/back_xy.bmp", 0, 0, 255, 255, 255);
    usleep(500000);
    init_homepage();
    *flag = 0;
    cleardate();
    return;
  }

  if (over == 0)
  {
    if (who == 0)
    {
      black(x, y, qp, &who, &over);
      if (over != 0)
      {
        winnerpage(&over);
      }

      return;
    }

    if (who == 1)
    {
      white(x, y, qp, &who, &over);
      if (over != 0)
      {
        winnerpage(&over);
      }

      return;
    }
  }
  else
  {
    //game end
    winnerpage(&over);
  }
}

void movie_event(int x, int y, int *flag)
{
  if (x >= 0 && x <= 50 && y >= 0 && y <= 50)
  {
    // back
    *flag = 0;
    system("killall -STOP madplay &");
    display_bmp("./bmp/movie/back_xy.bmp", 0, 0, 255, 255, 255);
    usleep(500000);
    display_bmp("./bmp/movie/back_xy.bmp", 0, 0, 255, 255, 255);
    system("killall -9 madplay");
    init_homepage();
  }
}

void home_event(int x, int y, int *flag)
{

  if (y >= 140 && y <= 340)
  {
    if (x >= 50 && x <= 250)
    {

      display_bmp("./bmp/home/xy/movie.bmp", 50, 140, 0, 0, 0);
      usleep(500000);
      display_bmp("./bmp/home/movie.bmp", 50, 140, 0, 0, 0);
      *flag = 2;
      //*movie_play = 110;
      init_moviepage(flag);
    }

    if (x >= 300 && x <= 500)
    {
      display_bmp("./bmp/home/xy/game.bmp", 300, 140, 0, 0, 0);
      usleep(500000);
      display_bmp("./bmp/home/game.bmp", 300, 140, 0, 0, 0);
      *flag = 3;
      init_gamepage();
    }

    if (x >= 550 && x <= 750)
    {
      display_bmp("./bmp/home/xy/music.bmp", 550, 140, 0, 0, 0);
      usleep(500000);
      display_bmp("./bmp/home/music.bmp", 550, 140, 0, 0, 0);
      *flag = 1;
      init_musicpage();
    }
  }
}

int count;

void music_event(int x, int y, int *flag, int *event)
{

  if (x >= 0 && x <= 50 && y >= 0 && y <= 50)
  {
    // back
    *flag = 0;
    *event = 0;
    draw_music_back();
    system("killall -9 madplay");
  }

  if (x >= 500 && x <= 600 && y >= 200 && y <= 300)
  {
    if ((*event > 150) || *event == 0)
    {
      // show play
      draw_music_play();
      if (*event == 0)
      {
        *event = 101;
        count = find_music();
        printf("找到歌曲%d首\n", count);
      }
      if (*event > 150)
      {
        *event -= 50;
        system("killall -CONT madplay &");
        draw_music_pause();
        return;
      }

      draw_music_pause();

      play_music(1);

      return;
    }

    if (*event < 150 && *event >= 100)
    {
      // show pause
      *event += 50;
      system("killall -STOP madplay &");
      draw_music_play();
      // pause
      return;
    }
  }
  if (x >= 420 && x <= 480 && y >= 220 && y <= 280)
  {
    draw_prev_xy();
    // prev

    if (*event == 0 && count > 1)
    {
      play_music(1);
      *event = 101;
      return;
    }
    system("killall -9 madplay");
    if (*event < 150 && *event > 100)
    {
      int index = *event;
      index -= 100;
      if (index == 1)
      {
        index = 1;
        *event = 101;
      }
      else
      {
        index -= 1;
        *event -= 1;
      }

      draw_music_play();
      usleep(500000);
      draw_music_pause();
      play_music(index);
    }
  }

  if (x >= 620 && x <= 680 && y >= 220 && y <= 280)
  {
    draw_next_xy();
    // next
    if (*event == 0 && count > 1)
    {
      play_music(1);
      *event = 101;
      return;
    }
    system("killall -9 madplay");
    if (*event < 150 && *event > 100)
    {
      int index = *event;
      index -= 100;
      if (index >= count)
      {
        index = 1;
        *event = 101;
      }
      else
      {
        *event += 1;
        index++;
      }

      draw_music_play();
      usleep(500000);

      draw_music_pause();
      play_music(index);
    }
  }
}

#endif
